/*
 * @Author: Jiangang LU
 * @Date: 2022-07-01 15:31:29
 * @LastEditors: Jiangang Lu
 * @LastEditTime: 2022-07-08 11:37:46
 * @Description: 单据跨源通信
 * a. 审批前调用，将待办状态变成审批中
 *    window.parent.postMessage('{"type": "approval-before", "data": data}', '*')
 * b. 审批失败后调用，将待办状态还原成待办
 *    window.parent.postMessage('{"type": "approval-fail", "data": data}', '*')
 * c. 审批成功后，刷新一下统一审批页面
 *    window.parent.postMessage('{"type": "approval-after", "data": data}', '*')
 * 其中 data 为单据数据包括
 * formCode、formName、formType
 */
const postMessage = (message = {}) => {
    window.parent.postMessage(JSON.stringify(message), '*');
};

const receiptPostMessage = (data) => {
    const approvalBefore = () => {
        postMessage({
            type: 'approval-before',
            data
        });
    }

    const approvalFail = () => {
        postMessage({
            type: 'approval-fail',
            data
        });
    }

    const approvalAfter = () => {
        postMessage({
            type: 'approval-after',
            data
        });
    }

    return {
        approvalBefore,
        approvalFail,
        approvalAfter
    };
};

export default receiptPostMessage;